<?php

namespace App\Admin\Controllers;

use App\Admin\Selectable\BusinessTimes;
use App\Admin\Selectable\Rooms;
use App\Admin\Selectable\Setmeals;
use App\Models\Order;
use App\Models\Store;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Database\Eloquent\Collection;

class OrderController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '订单列表';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Order());

        $grid->column('store.detail', '门店')->display(function ($item) {
            return \Arr::get($item, 'name', '');
        });

        $grid->column('name', __('Name'));
        $grid->column('mobile', __('Mobile'));
        $grid->column('time', __('预定时间'));
        $grid->column('price', __('Price'))->display(function ($data) {
            return $data = '￥'.$data;
        });
        $grid->column('receive_price', __('Receive price'))->display(function ($data) {
            return $data = '￥'.$data;
        });
        $grid->column('uncollected_price', __('Uncollected price'))->display(function ($data) {
            return $data = '￥'.$data;
        });
        $grid->column('print_order', __('Print order'))
            ->using([1 => '未打印', 2 => '已打印'])->dot([1 => 'danger', 2 => 'success']);

        $grid->column('cashier.cahiser_name', '收银员')->display(function($item) {
            return \Arr::get($item, 'name', '');
        });
        $grid->column('created_at', __('Created at'))->sortable();
        $grid->column('start_time', __('Start time'))->display(function ($start_time){
            return $start_time==''?'0':date('Y-m-d H:i:s',$start_time);
        });
        $grid->disableCreateButton();
        $grid->actions(function ($actions) {
            // 去掉查看
            $actions->disableEdit();
            // $actions->disableView();
        });
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();

            $filter->column(1/2, function ($filter){
                $filter->like('name', __('Name'));
                $filter->like('mobile', __('Mobile'));
                $filter->equal('store_id', '门店')
                ->select(Store::with(['detail'])->get()->pluck('detail.name', 'id'));
            });
            $filter->column(1/2, function ($filter){
                $filter->equal('print_order', __('Print order'))
                ->radio(['' => '全部',1 => '是', 2 => '否']);
                $filter->timestampBetween('time', '预定时间')->datetime();
            });
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Order::findOrFail($id));
        $show->panel()
        ->tools(function ($tools) {
            $tools->disableEdit();
            $tools->disableDelete();
        });;

        $show->field('store.name', __('Store id'));
        $show->field('name', __('Name'));
        $show->field('mobile', __('Mobile'));
        $show->field('time', '预定时间');
        $show->room('房间', function($room){
            //$room->setResource('/rooms');
            $room->panel()
            ->tools(function ($tools) {
                $tools->disableEdit();
                $tools->disableList();
                $tools->disableDelete();
            });;
            $room->number('房间号');
        });
        $show->field('set_meals_id', '套餐');
        $show->ordertogoods('商品', function($goods){
        	$goods->disableActions();
        	$goods->disableTools();
        	$goods->disableCreateButton();
        	$goods->disableFilter();
        	$goods->disableExport();
        	$goods->disableRowSelector();
            $goods->column('goods.name', '商品');
            $goods->column('number', '数量');
        });
        $show->businesstime('时间段', function ($businesstime) {
			$businesstime->panel()
            ->tools(function ($tools) {
                $tools->disableEdit();
                $tools->disableList();
                $tools->disableDelete();
            });;
        	$businesstime->start('开始')->as(function ($time){
	            if ($time>=86400-8*3600){
	                return '次日'.date('H:i',$time);
	            }
	            return date('H:i',$time);
	        });
        	$businesstime->end('结束')->as(function ($time){
	            if ($time>=86400-8*3600){
	                return '次日'.date('H:i',$time);
	            }
	            return date('H:i',$time);
	        });
        });
        $show->field('price', __('Price'));
        $show->field('receive_price', __('Receive price'));
        $show->field('uncollected_price', __('Uncollected price'));
        $show->field('print_order', __('Print order'))->as(function ($data) {
            return $data==1?"已打印":"未打印";
        });
        $show->field('description', __('Description'));
        $show->field('created_at', __('Created at'));
        $show->field('number', __('Number'));
        $show->field('start_time', __('Start time'))->as(function ($data) {
            return date('Y-m-d H:i:s', $data);
        });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Order());

		$form->column(1/2, function ($form) {
			$form->belongsTo('business_time_id', BusinessTimes::class, '时间段')->rules("required");
			// $form->belongsTo('scale_id', Scales::class, '房型')->rules("required");
	        $form->belongsTo('room_id', Rooms::class, '房型')->rules("required");
			// $form->belongsTo('storey_id', Storey::class, '楼层')->rules("required");
			//	$form->belongsTo('set_meals_id', Setmeals::class, '套餐')->rules("required");
			//	$form->hasMany('ordertogoods', '商品', function(Form\NestedForm $form){
			//	$form->orderGoodsText('goods_id', '商品名称');
			//	$form->text('number', '数量');
			//	});
		});
		$form->column(1/2, function ($form) {
			$form->hidden('store_id', 'Store_id');
	        $form->text('name', __('Name'))->rules("required");
	        $form->mobile('mobile', __('Mobile'))->rules("required|regex:/^1[34578]{1}\d{9}$/");
	        $form->datetime('time', __('Time'))->rules("required");
	        
			// $form->select('set_meals_id', '套餐')->options(
			// SetMeal::where('store_id', Admin::user()->store_id)->get()->pluck('name', 'id')
			// )->required();
	        $form->currency('price', __('Price'))->symbol('￥')->rules("required");
	        $form->currency('receive_price', __('Receive price'))->symbol('￥');
	        $form->currency('uncollected_price', __('Uncollected price'))->symbol('￥');
	        //$form->radio('print_order', __('Print order'))->options([1 => '未打印', 2 => '已打印']);
	        $form->text('description', __('Description'))->rules("required");
	        $form->text('number', __('Number'))
	            ->rules(function ($form) {
	                // 如果不是编辑状态，则添加字段唯一验证
	                if (!$id = $form->model()->id) {
	                    return 'unique:orders,number';
	                }
	            });
	        $form->datetime('start_time', __('Start time'));
		});
        

        $form->saving(function (Form $form) {
            $form->store_id = Admin::user()->store_id;
            $form->start_time = strtotime($form->start_time);
            $form->time = strtotime($form->time);
        });

        $form->footer(function ($footer) {
            $footer->disableViewCheck();
        });
        $form->tools(function ($tool) {
            //表单bottom
            $tool->disableView();
        });
        //表单bottom
        $form->disableReset();
        $form->disableEditingCheck();
        $form->disableViewCheck();
        $form->disableCreatingCheck();
        $form->disableSubmit();

        return $form;
    }
}
